<template>
  <a-row class="login">
    <a-col :span="8" :offset="8" class="login-main">
      <div class="login-header">
        <h1>注册页面</h1>
      </div>
      <a-form
          :model="loginForm"
          name="basic"
          autocomplete="off"
      >
        <a-form-item
            label=""
            name="user_name"
            :rules="[{ required: true, message: '请输入用户名!' }]"
        >
          <a-input v-model:value="loginForm.user_name" placeholder="请输入用户名"/>
        </a-form-item>
        <a-form-item
            label=""
            name="email"
            :rules="[{ required: true,  message: '请输入邮箱!' }]"
        >
          <a-input v-model:value="loginForm.email" type="email" placeholder="请输入邮箱"/>
        </a-form-item>
        <a-form-item
            label=""
            name="password"
            :rules="[{ required: true, message: '请输入密码!' }]"
        >
          <a-input v-model:value="loginForm.password" type="password" placeholder="请输入密码"/>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="register">注册</a-button>
        </a-form-item>
      </a-form>
    </a-col>
  </a-row>
</template>

<script>
import { defineComponent, reactive } from 'vue';
import axios from 'axios';
import { notification } from 'ant-design-vue';
import router from '@/router';

export default defineComponent({
  name: 'login-view',
  setup() {
    const loginForm = reactive({
      user_name: '',
      email: '',
      password: '',
    });

    const register = () => {
      console.log(loginForm);
      axios.post('/user/register', loginForm).then((response) => {
        const data = response.data;
        if (data.code === 0) {
          notification.success({description: '注册成功！'});
          router.push('/login');
          console.log('data内容：', data);
        } else {
          notification.error({description: data.message});
        }
      });
    };

    return {
      loginForm,
      register,
    };
  },
});
</script>

<style scoped>
.login {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  /* 添加背景图片 */
  background-image: url('../assets/register.jpg');
  /* 设置背景图片的其他属性 */
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-color: #f5f7fa; /* 如果图片加载失败，设置一个备用背景色 */
}

.login-main {
  padding: 30px;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  background-color: rgba(255, 255, 255, 0.8); /* 半透明背景 */
  width: 300px;
}

.login-header {
  text-align: center;
  margin-bottom: 20px;
}

.login-header h1 {
  font-size: 24px;
  color: #1890ff;
}

.ant-input {
  margin-bottom: 10px;
}
</style>